angular.module('Measure')
    .controller('Measure.MeasureCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', 'Http',
        function ($scope, $state, $http, $ionicLoading, $ionicPopup, Http) {
            $scope.imgPath = {
                cycle: u9ImHd.getFullPath('/img/cycle.png', 'Measure'),
                weight: u9ImHd.getFullPath('/img/weight.png', 'Measure')
            };
            $scope.Barcode = 'Item00000001';
            $scope.init = function (e) {
                //     e.stopPropagation ? e.stopPropagation() : "";
                getQcInfo($scope.Barcode);
            };


            $scope.getQc = false;
            $scope.Works = [];
            $scope.workSelected = $scope.Works[0];

            $scope.submit = function () {
                if (!$scope.qcStandard[0].NormValue || !$scope.qcStandard[1].NormValue) {
                    alert('卷制测量还未有数据哦');
                    return;
                }
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/setWrapperMeasure',
                    data: {
                        Parameter: {
                            Barcode: $scope.Barcode,
                            TerminalUniqueID: '11:33:55:77',
                            Work: $scope.workSelected._id || 0,
                            WorkName: $scope.workSelected.Name,
                            Users: $scope.Users,
                            QCRecords: $scope.qcStandard
                        }
                    }
                }).then(function () {
                    $ionicLoading.hide();
                    $scope.getQc = true;
                    showConfirm('提交成功', '为了保证程序安全，如不再需要修改或继续，请手动退出');
                }, function (err) {
                    $ionicLoading.hide();
                })['finally'](function () {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/getWrapperInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            TerminalUniqueID: '11:33:55:77'
                        }
                    }
                }).then(function (data) {
                    $scope.getQc = true;
                    operateBarCode(data);

                }, function (err) {

                })['finally'](function () {
                    $ionicLoading.hide();
                });
            }

            function operateBarCode(data) {
                $scope.barcode = data.basicInfo;
                $scope.qcStandard = data.qcStandard;
                $scope.Users = data.basicInfo.Users;
            }

            function showConfirm(title, temp) {
                var confirmPopup = $ionicPopup.alert({
                    title: title,
                    template: temp
                });
                confirmPopup.then(function (res) {
                    if (res) {

                    } else {

                    }
                });
            }


            $scope.init();
        }
    ]);